<template>
    <div id="ArriveListInformation"
        :style="{ 'transition': isCollapse ? '.6s all' : '.35s all', 'width': isCollapse ? myWidth1 : myWidth2 }">
        <div class="main" style="height:300px">
                <el-steps align-center>
                    <el-step  @click="forUpdate" v-for="item in list" :key="item.name" :title="item.name" :description="item.content" >
                    </el-step>
                </el-steps>
        </div>
        <div class="button">
            <el-button type="primary" @click="forAdd">添加流程</el-button>
            <el-button type="primary" @click="forChange">修改流程</el-button>
        </div>
        <!-- 添加数据的弹窗 -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-form ref="newData" :rules="rules"  :model="newData" label-width="80px">
                <el-form-item label="流程名称" prop="name">
                    <el-input v-model="newData.name"></el-input>
                </el-form-item>
                <el-form-item label="流程描述"  prop="content">
                    <el-input v-model="newData.content"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="(dialogVisible = false)">取 消</el-button>
                <el-button type="primary" @click="onAdd('newData')" >确 定</el-button>
            </span>
        </el-dialog>
        <!-- change数据的弹窗 -->
        <el-drawer title="编辑流程" :visible.sync="drawer" :direction="direction" :before-close="handleClose"  size="50%">
            <el-table :data="list" style="width: 100%">
                <el-table-column prop="name" label="日期">
                </el-table-column>
                <el-table-column prop="content" label="姓名">
                </el-table-column>
                <el-table-column  label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="forUpdate(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="forDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-drawer>
        <!-- update数据的弹窗 -->
        <el-drawer title="编辑流程" :visible.sync="drawer2" :direction="direction" :before-close="handleClose" >
            <el-form ref="newData"  :model="newData" label-width="80px">
                <el-form-item label="流程名称" prop="name">
                    <el-input v-model="newData.name"></el-input>
                </el-form-item>
                <el-form-item label="流程描述" prop="content">
                    <el-input v-model="newData.content"></el-input>
                </el-form-item>
            </el-form>
            <el-button type="primary" @click="onUpdate()">确 定</el-button>
            <el-button @click="(drawer2 = false,cleanNewData())">取 消</el-button>
                
        </el-drawer>
        
    </div>
</template>
<script>
import axios from 'axios';
export default {
    data(){
        return{
            direction: 'rtl',
            newData:{
                pid:"",
                name:"",
                content:"",
            },
            drawer: false,
            drawer2: false,
            dialogVisible: false,
            list:{},
            rules: {
                name: [
                    { required: true, message: '请输入内容', trigger: 'blur' },
                ],
                content: [
                    { required: true, message: '请输入内容', trigger: 'change' }
                ],
            },
            myHeight: "",
            myWidth1: "",//未展开的宽度
            myWidth2: "",//展开了的宽度
        }
    },
    props: {
        isCollapse: true,
    },
    methods:{
        forAdd(){
            this.cleanNewData();
            this.dialogVisible= !this.dialogVisible;
        },
        onAdd(newData) {
            
            this.$refs[newData].validate((valid) => {
                
                if (valid) {//校验成功
                    console.log(this.newData)
                    this.dialogVisible = false;
                    var _this=this;
                    axios({
                        method: "post",
                        url: "api/processes/addProcess",
                        data:this.newData,
                    }).then(function (resp) {
                        console.log(resp.data);
                        // _this.studentdetail = resp.data.data;
                        _this.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                        _this.init();
                        
                    })
                    
                } else {//校验失败
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        forChange(){
            this.drawer=!this.drawer;
        },
        forUpdate(index,row){
            // console.log(row);
            this.newData.name=row.name;
            this.newData.content=row.content;
            this.newData.pid=row.pid;
            this.drawer2 = !this.drawer2;
        },
        onUpdate(){
            if(this.newData.name==""||this.newData.content==""){
                this.$message("请输入规范内容");
            }else{
                var _this=this;
                axios({//获取步骤列表
                    method: "put",
                    url: "api/processes/updateProcess",
                    data:_this.newData,
                }).then(function (resp) {
                    if (resp.data.code == 200) {
                        _this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                        _this.init();
                    } else {
                        _this.$message("修改失败失败");
                    }
                    _this.drawer2 = !_this.drawer2;
                })
            }
            console.log(this.newData);
        },
        forDelete(index,row){
            var _this=this;
            // console.log(row);
            axios({//获取步骤列表
                method: "delete",
                url: "api/processes?pid="+row.pid,
            }).then(function (resp) {
                if(resp.data.code==200){
                    _this.$message({
                        message: '添加成功',
                        type: 'success'
                    });
                    _this.init();
                }else{
                    _this.$message("删除失败");
                }
                _this.drawer=!_this.drawer;
            })
        },
        cleanNewData(){
            this.newData.name = "";
            this.newData.content = "";
            // console.log("执行了");
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => { });
        },
        init(){
            var _this = this;
            axios({//获取步骤列表
                method: "get",
                url: "api/processes/getAllProcesses",
            }).then(function (resp) {
                // console.log(resp);
                _this.list = resp.data.data;
                // console.log(_this.list);
            })
        }
        
    },

    mounted() {
       this.init();
    },
    created() {
        this.myHeight = window.innerHeight - 40 - 40 - 20 - 30 + "px";
        this.myWidth1 = window.innerWidth - 64 - 30 + "px"
        this.myWidth2 = window.innerWidth - 200 - 30 + "px"
    },
    
}
</script>
<style scoped>
#ArriveListInformation {
    float: left;
    /* height: 500px; */
}
.main{
    width: 100%;
    margin-top: 15px;
    background-color: #ffffff;
    padding-top: 20px;
    overflow-x: scroll;
}

.button{
    width: 100%;
    height: 50px;
    background-color: #ffffff;
    border-top: 0.7px solid rgba(0, 0, 0, .2);
    line-height: 50px;
    text-align: left;
    text-indent: 50px;
}
</style>